package com.scalar.forecast.projectSetup.actionCommands;

import com.scalar.forecast.actionCommands.ActionCommand;
import com.scalar.forecast.projectSetup.dao.ProjectSetupDAO;
import com.scalar.forecast.projectSetup.MapPartNumbers;
import com.scalar.forecast.projectSetup.ReplacePartNumber;
import com.scalar.forecast.projectSetup.forms.MapPartNumbersForm;
import com.scalar.forecast.commons.SessionKeys;

import com.scalar.chiptrack.users.User;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionForm;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.ArrayList;
import java.util.HashMap;


/**
 * Process the request to keep the mapped Project Setup Part Numbers in scope and directs to confirm page
 *
 * Created by Veeru.
 * Date: Oct 10, 2005
 * Time: 5:44:11 PM
 */

public class MapPartNumbersSaveCommand extends ActionCommand {

    public ActionForward execute( ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response ) {

        ActionForward actionForward = mapping.findForward( "confirmMapPartNumbers" );
        MapPartNumbersForm mapPartNumbersForm = ( MapPartNumbersForm )form;
        MapPartNumbers mapPartNumbers = mapPartNumbersForm.getMapPartNumbers();
        HashMap errors = new HashMap ();
        request.setAttribute( SessionKeys.ERROR_MESSAGES, errors );
        HttpSession session = request.getSession ();
        User user = ( User ) session.getAttribute ( com.scalar.chiptrack.commons.SessionKeys.USER_KEY );

        ArrayList chipsList = mapPartNumbers.getChipsList();
        ArrayList boardsList = mapPartNumbers.getBoardsList();
        ArrayList kitsList = mapPartNumbers.getKitsList();
        ReplacePartNumber partNumber;
        try
        {
            for (int i = 0; i < chipsList.size(); i++) {
                partNumber = (ReplacePartNumber) chipsList.get(i);
                partNumber.setReplacingPartNumber( ProjectSetupDAO.getPartNumber( null, partNumber.getReplacingPartNumberId() ));
            }
            for (int i = 0; i < boardsList.size(); i++) {
                partNumber = (ReplacePartNumber) boardsList.get(i);
                partNumber.setReplacingPartNumber( ProjectSetupDAO.getPartNumber( null, partNumber.getReplacingPartNumberId() ));
            }
            for (int i = 0; i < kitsList.size(); i++) {
                partNumber = (ReplacePartNumber) kitsList.get(i);
                partNumber.setReplacingPartNumber( ProjectSetupDAO.getPartNumber( null, partNumber.getReplacingPartNumberId() ));
            }
            if ( ProjectSetupDAO.isProjectHasForecastEntry ( null, mapPartNumbersForm.getProjectId() ) )
            {
                request.setAttribute ( "isForecastEntered", "true" );
            }
        }
        catch( Exception e )
        {
            e.printStackTrace();
        }

        if ( chipsList.size() > 0 )
        {
            request.setAttribute("showChips", "TRUE");
        }
        if ( boardsList.size() > 0 )
        {
            request.setAttribute("showBoards", "TRUE");
        }
        if ( kitsList.size() > 0 )
        {
            request.setAttribute("showKits", "TRUE");
        }
        return actionForward;
    }
}
